﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MegavaniaX.World;
using MegavaniaX.Messaging.Events;


namespace MegavaniaX.Actors.States
{
    public class PlayerStateIdle : PlayerStateBase
    {
        private static MegavaniaEventType[] _eventsCanHandle = new MegavaniaEventType[]
        {
            MegavaniaEventType.JumpPressed,
            MegavaniaEventType.MoveLeftPressed,
            MegavaniaEventType.MoveRightPressed,
            MegavaniaEventType.ClimbPressed,
            MegavaniaEventType.SlidePressed,
            MegavaniaEventType.ShootPressed
        };        

        public PlayerStateIdle(Player player) : base(player)
        {
        }
        

        public override void OnEnter()
        {
            _player.CurrentAnimation = _player.animationIdle;
        }
        
        public override bool Update(TimeSpan elapsedTime, Screen screen)
        {
            if (base.Update(elapsedTime, screen))
                return true;

            if (!_player.IsActorOnSolidGround(screen))
            {
                NextState = new PlayerStateJump(_player, 0.0f);
                return true;
            }
            return false;
        }

        public override bool CanHandleEvent(MegavaniaEventType evtType)
        {
            return (_eventsCanHandle.Contains(evtType));
        }

        public override bool HandleEvent(IMegavaniaEvent evt)
        {
            if (evt.EventType == MegavaniaEventType.JumpPressed)
            {
                NextState = new PlayerStateJump(_player, Player.InitialYVelo);
                return true;
            }
            // movement
            else if (evt.EventType == MegavaniaEventType.MoveRightPressed)
            {
                NextState = new PlayerStateMove(_player, Player.InitialXVelo);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.MoveLeftPressed)
            {
                NextState = new PlayerStateMove(_player, -Player.InitialXVelo);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.ShootPressed)
            {
                _player.CurrentAnimation = _player.animationShootIdle;
            }
            else if (evt.EventType == MegavaniaEventType.SlidePressed)
            {
                NextState = new PlayerStateSlide(_player, Player.InitialXVelo * 1.5f);
                return true;
            }
            else if (evt.EventType == MegavaniaEventType.ClimbPressed)
            {
                var cevt = (ClimbEvent)evt;
                if (HandleClimbEvent(cevt))
                    return true;
            }

            return false;
        }

        public override void StopShootingAnimation()
        {
            _player.CurrentAnimation = _player.animationIdle;
        }

        public override void OnExit()
        {
            // nothing to do
        }
    }
}
